package cn.com.oceansoft.business.szjf.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.sys.entity.SysUser;
import cn.com.oceansoft.business.sys.service.ISysUserService;
import cn.com.oceansoft.business.szjf.entity.Sms;
import cn.com.oceansoft.business.szjf.service.ISmsService;
import cn.com.oceansoft.business.szjf.service.IZhshService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Administrator on 2017/6/26.
 * 注册账号控制类
 */
@Controller
@RequestMapping("zhsh")
public class ZhshController extends BaseController {
    @Resource
    private IZhshService zhshService;
    @Resource
    private ISmsService smsService;
    @Resource
    private ISysUserService sysUserService;

    /**
     * 进入账号审核列表页
     * @param model
     * @return
     */
    @RequestMapping(value = "index")
    public String gotoZhsh(Model model,Query query,SysUser user){
        //List<SysUser> zczhs = zhshService.getZczh("0");//获取没有审核的注册账号
        //model.addAttribute("zczhs",zczhs);
        user.setTemp2("0");
        query.setObj(user);
        Pager<SysUser> pager = zhshService.findZczhByPage(query);
        model.addAttribute("pager",pager);
        model.addAttribute("user",user);
        return "szjf/zhsh/zhsh";
    }

    /**
     * 获取审核列数据
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMore")
    public Result getZhsh(Model model,Query query,SysUser user){
        user.setTemp2("0");
        query.setObj(user);
        Pager<SysUser> pager = zhshService.findZczhByPage(query);
        return  new Result(Result.SUCC,pager);
    }

    /**
     *注册账号通过
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "approval")
    public Result approval(SysUser user){
        try {

            zhshService.approvalByGuid(user);
            SysUser user1 = sysUserService.findUserById(user.getGuid());
            String smsContent = user1.getRealname()+"您好！您的注册的【"+user1.getUsername()+"】账号已经通过审核，可以到网站进行登录";
            int i = smsService.sendMsm(user1.getPhone(),smsContent);
            //短信存入SMSRecords表
            Sms sms = new Sms();
            sms.setSmsno(smsService.getNewSmsNo());
            sms.setReceivephone(user1.getPhone());
            sms.setRecipient(user1.getRealname());
            sms.setSender("管理员");
            sms.setSendtime(new Date());
            sms.setSmstype("0");//1通知类，0回访类
            sms.setSmstitle("");
            sms.setSmscontent(smsContent);
            sms.setSmsstate(i);//0发送失败，1发送成功， 2未发送
            sms.setIstiming("0");
            smsService.insertSMSRecords(sms);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(Result.FAILURE,"操作失败");
        }
        return  new Result(Result.SUCC,"操作成功");
    }
}
